package com.atguigui.leetcode1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1630.等差子数组
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/23 8:51
 */
public class P1630ArithmeticSubarrays {
    public static void main(String[] args) {
        Solution solution = new P1630ArithmeticSubarrays().new Solution();
        // TO TEST
    }

    class Solution {
        public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
            ArrayList<Boolean> ans = new ArrayList<>();
            for (int i = 0; i < l.length; i++) {
                //拷贝出数据
                int arr[] = Arrays.copyOfRange(nums, l[i], r[i] + 1);
                //判断是否能组成等差数组
                Arrays.sort(arr);
                int d = arr[1] - arr[0];
                boolean ch = true;
                //循环遍历判断是否都等差
                for (int j = 2; j <= r[i] - l[i]; j++) {
                    if (d != arr[j] - arr[j - 1]) {
                        ch = false;
                        break;
                    }
                }
                ans.add(ch);
            }
            return ans;
        }
    }
}
